package vue;


import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import metier.DefaultHttpClientPerso;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.net.ParseException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.finalprojet.R;

public class SignIn extends Activity {
	
	private EditText Login = null;
	private EditText Email = null;
	private EditText Pass = null;
	private EditText Pass2 = null;
   

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.signin);
        
        
        Button Signin = (Button)findViewById(R.id.SignIn);
        Button Retour = (Button)findViewById(R.id.ret);
       
        
      Login = (EditText)findViewById(R.id.Login);
      Email = (EditText)findViewById(R.id.Email);
      Pass = (EditText)findViewById(R.id.Password);
      Pass2 = (EditText)findViewById(R.id.Password2);
        
        
		
		
        
        Signin.setOnClickListener(new OnClickListener() 
        {
			
			public void onClick(View v) 
			
				{
				
				
				

		        
		        
		        String login = Login.getText().toString();
		        String pass1 = Pass.getText().toString();
		        String pass2 = Pass2.getText().toString();
		        String email = Email.getText().toString();
		       // String verifEmail = "/^[a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,6}$/";
		        Pattern p = Pattern.compile("^[a-z0-9._-]+@[a-z0-9._-]+\\.[a-z]{2,6}$");
		        Matcher m = p.matcher(email);
		        
		        
		        boolean erreur = false;
		        String err = "";
		        
		       if (login.isEmpty() || pass1.isEmpty() || pass2.isEmpty() || email.isEmpty() ) { erreur = true ; err+="Vous devez remplir les champs du formulaire\n";}
 
		       if ( !pass1.equals(pass2) ) {erreur = true; err+="Mot de passe diff�rent\n";}
		       
		       if ( !m.find() ) {erreur = true; err+="Email invalide\n";}
		       
		       if ( pass1.length() < 8 || pass2.length() < 8 ) {erreur = true; err+="Le Mot de passe doit comporter au moins 8 caract�res";}
		        	
		       if ( erreur ) Toast.makeText(getApplicationContext(), err, Toast.LENGTH_LONG).show();
		       
		       else
		        
		       
		        {
		    	   HttpTask req = new HttpTask();
	        		
		    	   String[] parametre = {login,email,pass1};
		       
		    	   req.execute(parametre);
		        	
		        }
		        
		        
		        
		        
		        
		        
		     
		      
		        
		        	
		        		
		        		
		        	
		        
				}
		});
        
        
        
        Retour.setOnClickListener(new OnClickListener() 
        {
			
			public void onClick(View v) 
			
				{
				
				Intent nextScreen = new  Intent (getApplicationContext (), EcranAccueil.class );
				startActivity (nextScreen);
		        
				}
		});
        
        
        
       
		
    
        
        }
    
    
    
    









	public class HttpTask extends AsyncTask< String[], Void, String>   
    
{



    protected String doInBackground(String[]... arg0) {
	
	
	
	
	//String url  =  (String)arg0[0][1];
	
	 //Toast.makeText(getBaseContext(),"deb",Toast.LENGTH_LONG).show();	 
	
	//String id = "15";
	//String data = "Maxime";
	
	
	
	
	
	
	DefaultHttpClientPerso httpclient = new DefaultHttpClientPerso();
	
	String[] parametre = arg0[0];
	
	String pseudo = parametre[0];
	String email = parametre[1];
	String password = parametre[2];
	
	
	HttpGet httpget = new HttpGet("https://spartacus.iutc3.unicaen.fr/~projet.echec/android/inscription.php?pseudo="+pseudo+"&email="+email+"&password="+password);

	
	HttpResponse response = null;
	String responseString = null;
	
	
	 try {
	
		 response = httpclient.execute(httpget);
		 
	
	
	 } catch (ClientProtocolException e) {
	        e.printStackTrace();
	    } catch (IOException e) {
	       e.printStackTrace();
	    } 
	    
	    HttpEntity entity = response.getEntity();
	 
	 if ( entity != null )
		 try {
			 responseString = EntityUtils.toString(entity);
		 } catch (ParseException e) {
				e.printStackTrace();
		    } catch (IOException e) {
		    	e.printStackTrace();
		    }
	
	 return responseString;
	
		 	}

	
protected void onPostExecute(String result) {
	String toast = null;
	try {
		
		JSONObject main =new JSONObject(result);
		String erreur = main.get("error").toString();
		
		if ( erreur.contains("e") )
			
		{
			
			
			
			
			if (  erreur.contains("email") ) 
			{
				
				toast = "Email Existant";
				Toast.makeText(getApplicationContext(),toast,Toast.LENGTH_SHORT).show();
			}
			
			if (  erreur.contains("pseudo") )
			{
				
				
				toast = "Pseudo existant";
				Toast.makeText(getApplicationContext(),toast,Toast.LENGTH_SHORT).show();
			}
			
			
		}
		
		
		
			
		

		else 
			{
			toast = "Inscription effectu�  en tant que " + main.get("pseudo").toString();
			Toast.makeText(getApplicationContext(),toast,Toast.LENGTH_SHORT).show();
			Intent nextScreen = new  Intent (getApplicationContext(), EcranAccueil.class );
			//nextScreen.putExtra("id",main.get("id").toString() );
			nextScreen.putExtra("pseudo", main.get("pseudo").toString());
			startActivity (nextScreen);
			}
		
		

		
	} catch (Exception e) {
		Toast.makeText(getApplicationContext(),e.toString(),Toast.LENGTH_LONG).show();
	}
	
	
	

	
		
	}
	

	
	
		

}
    
    
    
    
    
}